<?php
#include(APPLICATION_PATH . DS . "/modules/home/model/Friend.php");
//Social Network Service helper functions
class Helper_Message extends Zend_Controller_Action_Helper_Abstract
{
    
    private $_ctrl;
    private $_prefix;
    
    public function init()
    {
        $this->_ctrl = $this->getActionController();    
        $this->_prefix = "message_box_";
    }
        
    //check user member ship
    public function sendMessage($senderID, $title, $content, $rIDArr)
    {
        
        if ($senderID < 1) return false;
        if (count($rIDArr) < 1) return false;

        // =======================
        try {
            $t = time();
            $mdl = AppGlobal::loadModel("message", "home");
            $data['senderID'] = $senderID;
            $data['title'] = $title;
            $data['receiver'] = $mdl->getReceiverName($rIDArr);
            $data['content'] = $content;  
            $data['last_reply_uid'] = $senderID;  
            $data['changed'] = $t;             
            $data['timestamp'] = $t;  
            $data['pid'] = 0;   
            $data['isDel'] = 0;
            
            $last_mid = $mdl->insert($data);

            // INSERT MESSAGE_RECEIVE TABLE
            $mr_mdl=AppGlobal::loadModel("MessageReceive", "home");
            $mr_mdl->insertdb($last_mid, $rIDArr);  
            
            // INSERT MESSAGE_RECEIVE TABLE
            if (!in_array($senderID, $rIDArr))
            {
                $mr_mdl->insertdb($last_mid, array($senderID), $t+1);
            }
                        
            return true;          
        }
        catch (Exception $e)
        {
            echo $e->getMessage();
            return false;
        }
    }
    
}

